//
//  NewsSettingsCell.m
//  KEREN
//
//  Created by MW on 2023/3/24.
//

#import "NewsSettingsCell.h"
#import "UIButton+Expand.h"
@implementation NewsSettingsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.bottomView.hidden = YES;
    self.rightBtn.hidden = YES;
    self.countLbl.hidden = YES;
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] init];
        [self addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.offset(0);
            make.height.offset(70);
        }];
    }
    return _bottomView;
}

- (void)setBottomViewWithIsBleLock:(BOOL)isBleLock andCurrentCount:(int)count{
    [self.btnArrM removeAllObjects];
    for (UIView *view in _bottomView.subviews) {
        [view removeFromSuperview];
    }
    [_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(isBleLock){
            make.height.offset(106);
        }else{
            make.height.offset(70);
        }
    }];
    NSArray *titleArr;
    NSArray *decArr;
    if(isBleLock){
        titleArr = @[@"1档",@"2档",@"3档",@"4档",@"5档"];
        decArr = @[@"近",@"",@"中",@"",@"远"];
    }else{
        titleArr = @[@"",@"",@"",@"",@""];
        decArr = @[@"低",@"",@"中",@"",@"高"];
    }
    for (int i = 0; i < 5; i ++) {
        UILabel *title = [[UILabel alloc] init];
        if(isBleLock){
            title.frame = CGRectMake( (DEVICE_WIDTH) / 5 * i, 0, (DEVICE_WIDTH) / 5 , 20);
        }else{
            title.frame = CGRectMake( (DEVICE_WIDTH) / 5 * i, 0, (DEVICE_WIDTH) / 5 , 0);
        }
        [_bottomView addSubview:title];
        title.text = titleArr[i];
        title.textAlignment = NSTextAlignmentCenter;
        title.font = Font(14);
        title.textColor = Color_333333;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bottomView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(title);
            make.width.height.offset(20);
            if(isBleLock){
                make.top.offset(36);
            }else{
                make.top.offset(0);
            }
        }];
        button.backgroundColor = Color_7ACC00;
        button.layer.cornerRadius = 10;
        button.clipsToBounds = YES;
        if(i == 1){
            UIView *lineView = [[UIView alloc] init];
            [_bottomView addSubview:lineView];
            lineView.backgroundColor = Color_7ACC00;
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(30);
                make.right.offset(-30);
                make.height.offset(2);
                make.centerY.equalTo(button);
            }];
        }

        if(isBleLock){
            if(i == count){
                [button mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.height.offset(30);
                    make.top.offset(31);
                }];
                button.layer.cornerRadius = 16;
            }
        }else{
            if((count == 0 && i == 0) ||(count == 1 && i == 2) || (count == 2 && i == 4)){
                [button mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.height.offset(30);
                    make.top.offset(-5);
                }];
                button.layer.cornerRadius = 16;
            }
            if(i == 1 || i == 3){
                button.hidden = YES;
            }
        }
        UILabel *dectitle = [[UILabel alloc] init];
        if(isBleLock){
            dectitle.frame = CGRectMake( (DEVICE_WIDTH) / 5 * i, 70, (DEVICE_WIDTH) / 5 , 20);
        }else{
            dectitle.frame =  CGRectMake( (DEVICE_WIDTH) / 5 * i, 34, (DEVICE_WIDTH) / 5 , 20);
        }
        [_bottomView addSubview:dectitle];
   
        dectitle.text = decArr[i];
        dectitle.textAlignment = NSTextAlignmentCenter;
        dectitle.font = Font(14);
        dectitle.textColor = Color_333333;
        button.tag = 1000 +i;
        button.hitEdgeInsets = UIEdgeInsetsMake(-20, -20, -20, -20);
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArrM addObject:button];

    }
}

- (void)btnClick:(UIButton *)sender{
    WEAKSELF
    if(self.isBleLock){
        int gear = (int)(sender.tag - 1000 + 1);
        [KeRenRequestTool deviceLockDistance:[UserInfoManager sharedInstance].currentDeviceInfo gear:gear bleSuccess:^(BOOL netSuccess, BOOL status) {
            [weakSelf changeUI:(int)sender.tag gear:gear];
        } failure:^(BOOL netFailure) {
                    
        }];
    }else{
        int gear = 1;
        if(sender.tag == 1002){
            gear = 2;
        }else if(sender.tag == 1004){
            gear = 3;
        }
        [KeRenRequestTool deviceVibrateSensitivity:[UserInfoManager sharedInstance].currentDeviceInfo gear:gear bleSuccess:^(BOOL netSuccess, BOOL status) {
            [weakSelf changeUI:(int)sender.tag gear:gear];
        } failure:^(BOOL netFailure) {
                    
        }];
    }
}

-(void)changeUI:(int)tag gear:(int)gear{
    if([self.delegate respondsToSelector:@selector(sureSwitch:gear:)]){
        [self.delegate sureSwitch:self gear:gear];
    }
    for (UIButton *btn in self.btnArrM) {
        if(btn.tag == tag){
            [btn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.height.offset(30);
                if(self.isBleLock){
                    make.top.offset(31);
                }else{
                    make.top.offset(-5);
                }
            }];
            btn.layer.cornerRadius = 16;
        }else{
            [btn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.height.offset(20);
                if(self.isBleLock){
                    make.top.offset(36);
                }else{
                    make.top.offset(0);
                }
            }];
            btn.layer.cornerRadius = 10;
        }
    }
}

- (BOOL)showError{
    if(![UserInfoManager sharedInstance].currentDeviceInfo.is_manager){
        [[KRAlartManager shareInstance] showAlert:@"该功能仅对车主开放"];
        return YES;
    }
    if([UserInfoManager sharedInstance].currentDeviceInfo.online_status == 0 && [[UserInfoManager sharedInstance].currentDeviceInfo is4GDevice]){
        [[KRAlartManager shareInstance] showErrorAlert:@"车辆已离线"];
        return YES;
    }
    return NO;
}




- (NSMutableArray *)btnArrM{
    if(!_btnArrM){
        _btnArrM = [NSMutableArray array];
    }
    return _btnArrM;
}
@end
